<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field.options.displayStyle"
  >
    <div class="scene-DRC-single-info-widget" id="widget" :style="getStyle('head')">
      <!--科室信息-->
      <div class="dept-room" :class="getDeptHideClassName">
        <div class="dept item" v-if="optionsInfo.checkList.includes(1)">
          <ScrollX>{{ getRoomDiffInfo || '\u00a0' }}</ScrollX>
        </div>
        <div class="dept item" v-if="optionsInfo.checkList.includes(2)">
          <ScrollX>{{ getParentRegisterObjName || '\u00a0' }}</ScrollX>
        </div>
        <div class="room item">
          <ScrollX>
            {{ getRoomDeptInfo[optionsInfo['roomNameKey']] || getRoomDeptInfo['roomName'] || '\u00a0' }}
          </ScrollX>
        </div>
      </div>
      <!--医生信息-->
      <div class="doctor-info flex-box" v-for="item in getRoomDoctorInfo">
        <div class="picture-info flex-box-left">
          <img :src="(item['workerPicture']) || '/tpleditor/resource/assets/images/header.png'" alt="医生">
        </div>
        <div class="doctor-patient-info flex-box-right">
          <div class="name-level">
            <p class="name" :style="getStyle('name')">
              <ScrollX>{{ (item['workerName']) || '' }}</ScrollX>
            </p>
            <p class="level" v-if="(item['workerTitle'])">
              <ScrollX>{{ (item.workerTitle) || '\u00a0' }}</ScrollX>
            </p>
          </div>
          <!--患者信息-->
          <div class="patient-info" v-if="true">
            <div class="patient-items flex-box current-patient" :class="getPatientClassName('current-patient')">
              <div class="title flex-box-left">{{ optionsInfo['currentPatientSign'] || '就诊' }}</div>
              <div class="content flex-box-right" :class="{'current-patient-info': scrollInfo(item)}">
                <template v-for="PatientItem in getCurrentPatientList(item.treatingPatient)">
                  <p class="patient-item">
                    {{ (PatientItem.orderNo) || '\u00a0' }}
                    {{ (PatientItem.patientName) || '\u00a0' }}
                  </p>
                </template>
              </div>
            </div>
            <div class="patient-items flex-box wait-patient" :class="getPatientClassName('wait-patient')">
              <div class="title flex-box-left">
                {{ optionsInfo['waitPatientSign'] || '待诊' }}
              </div>
              <div class="content flex-box-right">
                <template v-for="(item) in getWaitPatientList(item.waitingPatientList)">
                  <p class="patient-item" :key="item.patientIdNo">
                    {{ item.orderNo || '\u00a0' }} {{ item.patientName || '\u00a0' }}
                  </p>
                </template>
              </div>
            </div>
          </div>
        </div>

      </div>

    </div>
  </CompOperate>
</template>

<script>
import {Popup} from 'vant';
import {pxToVw} from '@/utils/common';
import emitter from "@/utils/designer/emitter";
import vueSeamlessScroll from 'vue-seamless-scroll';
import {buildParentApi} from "@/utils/designer/util";
import ScrollX from '@c/public/seamlessScroll/ScrollX.vue'
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";

let buildParent = buildParentApi();
export default {
  name: "scene-DRC-single-info-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    CompOperate,
    ScrollX,
    Popup,
    vueSeamlessScroll
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi()
    },
  },

  data() {
    return {
      show: true,
      visible: true,
      optionsInfo: Object(),
      header: require("@a/images/design/header.jpeg")
    }
  },

  computed: {

    // 判断当前患者是否滚动
    scrollInfo() {
      return function (info) {
        return !!(info.treatingPatient) && (info.treatingPatient.length) > 1
      }
    },

    optionScroll() {
      return {
        hoverStop: true,
        direction: 1,                               // 0向下 1向上 2向左 3向右
        isSingleRemUnit: true,
        step: [1, 1.5, 2, 2.5, 3][0], // 数值越大速度滚动越快
        limitMoveNum: 2, // 开始无缝滚动的数据量 this.dataList.length
      };
    },

    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case 'head':
            Reflect.set(Obj, 'height', pxToVw(info.headHeight));
            Reflect.set(Obj, 'backgroundColor', `${(info.backgroundColor)}`);
            return Obj;
          case 'name':
            Reflect.set(Obj, 'fontWeight', (info.doctorFontWeight));
            return Obj;
          case 'picture':
            Reflect.set(Obj, 'height', pxToVw(info.imageHeight));
            Reflect.set(Obj, 'width', pxToVw(info.imageWidth));
            Reflect.set(Obj, 'borderRadius', pxToVw(info.imageRadius));
            return Obj;
        }
      }
    },

    getDeptHideClassName() {
      let {checkList, deptRoomPosition} = (this.optionsInfo);
      if (checkList && (checkList.length === 1) && checkList[0] === 3) {
        return 'dept-room-hide'
      } else {
        if (deptRoomPosition) {
          return 'dept-room-position'
        }
      }
    },

    // 设置不同模式下的样式
    getPatientClassName() {
      let {waitMode} = (this.optionsInfo);
      return function (type) {
        return `${type}-${waitMode}`;
      }
    },

    // 科室可能因为医生是否登录会有差异
    getRoomDiffInfo() {
      let list = (this.getRoomDoctorInfo);
      let deptInfo = (this.getRoomDeptInfo)
      if (list?.length) {
        return list[0].deptName || deptInfo?.deptName || '\u00a0'
      } else {
        return deptInfo?.deptName || '\u00a0'
      }
    },

    // 获取当前患者号别或原科室
    getParentRegisterObjName() {
      const self = this;
      let {checkList, registerObjNameKey, registerSource, relationRegister} = (self.optionsInfo);
      console.log(relationRegister);
      let boolean = checkList.includes(2);
      let boolean2 = checkList.includes(1);
      let listInfo = (this.getRoomDoctorInfo);
      let roomDeptInfo = (this.getRoomDeptInfo);
      if (boolean) {
        // 号别来源当前医生呼叫的患者
        if (registerSource === 2) {
          if (!!(listInfo?.length)) {
            let patientList = Array();
            if (listInfo[0]?.treatingPatient?.length) {
              patientList = listInfo[0].treatingPatient
            } else if (listInfo[0]?.preTreatingPatient?.length) {
              patientList = listInfo[0].preTreatingPatient
            } else {
              patientList = Array()
            }
            if (patientList?.length) {
              let registerObjNameInfo = Array();
              patientList.forEach((item) => {
                let temp = (item[registerObjNameKey] || item['registerObjName']);
                if (!!temp) {
                  registerObjNameInfo.push(temp)
                }
              })
              // 去重
              let registerObjName = self.$lodash.compact(registerObjNameInfo);
              if (!!registerObjName && !!(registerObjName.length)) {
                return self.$lodash.uniq(registerObjName).join(',')
              }
            } else {
              return '\u00a0'
            }
          } else {
            if (relationRegister) {
              return (roomDeptInfo?.registerObjName) || '\u00a0'
            } else {
              return '\u00a0'
            }
          }
        } else {
          // 号别来源当前医生所在科室号别
          return (roomDeptInfo?.registerObjName) || '\u00a0'
        }
      } else if (boolean2) {
        return '\u00a0'
      }
    },

    // 获取科室信息
    getRoomDeptInfo() {
      let devType = (this.devType);
      let roomDeptInfo = this.$store.getters['getRoomDeptInfo'];
      return devType ? roomDeptInfo.apiData : roomDeptInfo.mockData;
    },

    // 获取全量医患信息
    getRoomDoctorInfo() {
      let devType = (this.devType);
      let roomDoctorInfo = this.$store.getters['getRoomDoctorInfo'];
      let roomDoctorList = devType ? (roomDoctorInfo.apiData) : (roomDoctorInfo.mockData);
      let len = (roomDoctorList?.length);
      if (!!len) {
        switch (len) {
          case 0:
            return Array(1).fill({});
          case 1:
          default:
            return roomDoctorList.slice(0, 1);
        }
      } else {
        return Array(1).fill({});
      }
    },

    // 就诊患者
    getCurrentPatientList() {
      return function (row) {
        if (!!(row?.length)) {
          return row.slice(0, 1)
        } else {
          return [];
        }
      }
    },

    // 候诊患者截取
    getWaitPatientList() {
      return function (row) {
        if (!!(row?.length)) {
          let {waitMode} = (this.optionsInfo);
          return row.slice(0, waitMode)
        } else {
          return [];
        }
      }
    }
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "roomDeptInfo":
          this.$store.dispatch('setRoomDeptInfo', data)
          break
        case "roomDoctorInfo":
          this.$store.dispatch('setRoomDoctorInfo', this.$lodash.cloneDeep(data))
          break
      }
    },

    // 滚动结束
    handleScrollEnd() {
      console.log('滚动结束');
    },
  },

  watch: {
    field: {
      handler: function (val) {
        let key = (val.options.config)
        this.optionsInfo = val.options[key];
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },
};
</script>

<style lang="scss" scoped>
.scene-DRC-single-info-widget {
  color: #333;
  padding: 18px 30px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  background: transparent;
  overflow: hidden;
  position: relative;
  //科室
  .dept-room {
    padding: 20px 0 8px 0;
    background: #0059B3;
    height: 300px;
    border-radius: 20px;
    overflow: hidden;

    > div {
      color: #FFFFFF;
      text-align: center;
      font-size: 80px;
      padding: 0 30px;
    }

    .dept {
      font-size: 80px;
      line-height: 1.5;
      @include singleEllipsis(100%)
    }

    .room {
      font-size: 120px;
      line-height: 1.5;
      font-weight: normal;
    }
  }

  .dept-room-hide {
    display: flex;

    > div {
      width: 100%;
      align-self: center;
    }
  }

  // 科室诊室调整上下位置
  .dept-room-position {
    display: flex;
    flex-direction: column-reverse;

    .dept {
      font-size: 120px;
      font-weight: normal;
      line-height: 180px;
      min-height: 170px;
      @include singleEllipsis(100%);
    }

    .room {
      font-size: 80px;
      line-height: 120px;
      @include singleEllipsis(100%);
    }
  }

  //医生-患者信息
  .doctor-info {
    display: flex;
    margin: 20px 0 0 0;
    min-width: auto;
    max-height: 506px;
    height: 506px;

    .picture-info {
      flex: inherit;
      border-radius: 20px;
      overflow: hidden;
      flex-basis: 358px;
      min-width: 358px;
      border: none;
      height: 100%;
      min-height: 486px;
      align-self: flex-end;
      margin-right: 30px;
      background: transparent;

      img {
        width: 100%;
      }
    }

    .doctor-patient-info {
      flex: inherit;
      display: flex;
      flex-direction: column;
      width: calc(100% - 358px - 30px);
      border: none;
      height: 100%;
      align-self: flex-end;
      background: transparent;

      .name-level {
        display: flex;
        height: 80px;
        line-height: 80px;
        margin-top: 10px;
        margin-bottom: 30px;

        .name {
          font-size: 80px;
          height: 100%;
          flex-basis: 240px;
          max-width: 260px;
          @include fontWeight();
        }

        .level {
          font-size: 44px;
          color: #E8E8E8;
          flex-shrink: 1;
          height: 100%;
          white-space: nowrap;
          border-radius: 8px;
          padding: 4px 20px;
          overflow: hidden;
          margin-left: 30px;
          display: inline-block;
          background: #0059b3;
        }
      }

      //患者信息
      .patient-info {
        .patient-items {
          display: flex;
          overflow: hidden;
          border-radius: 12px;
          width: auto;
          min-width: auto;

          .title {
            height: 100%;
            display: flex;
            flex: inherit;
            align-items: center;
            align-self: center;
            text-align: center;
            color: white;
            padding: 0 30px;
            line-height: 1.32;
            width: 124px !important;
            font-size: 50px;
            border: none;
          }

          .content {
            display: flex;
            align-items: center;
            flex-direction: column;
            align-self: center;
            justify-content: left;
            background: transparent;
            border: none;
            overflow: hidden;
            width: calc(100% - 124px) !important;

            p.patient-item {
              font-size: 62px;
              padding-left: 40px;
              line-height: 86px;
              text-align: left;
              @include singleEllipsis(100%)
            }
          }
        }

        .current-patient {
          height: 140px;
          min-height: 140px;
          background: rgba(0, 89, 179, 0.2);

          .title {
            background: rgba(0, 89, 179, 1);
          }

          p {
            color: rgba(0, 89, 179, 1);
          }
        }

        .wait-patient {
          height: 206px;
          margin-top: 30px;
          min-width: auto;
          background: rgba(255, 198, 85, 0.2);

          .title {
            background: rgba(229, 134, 2, 1);
          }

          p {
            color: rgba(229, 134, 2, 1);
            line-height: 106px !important;
            @include singleEllipsis(100%)
          }
        }

      }
    }
  }

}
</style>

<style lang="scss">
.popup-class {
  height: 54% !important;
}

.van-popup {
  position: absolute;
}
</style>
